package com.lan.test01;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test06 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        //1.创建class对象
        Class clazz = Class.forName("com.lan.test01.Student");

        //2.获取成员方法
            //返回所有公共方法，包括继承的
            //method1(clazz);

            //返回所有成员方法，包括私有，不包括继承
            //method2(clazz);

            //返回单个公共成员方法对象
            //参数一：方法名
            //参数二：可变参数，传入方法形参类型的class类
            //method3(clazz);

        //返回单个成员方法对象,可以是私有
        Method method = clazz.getDeclaredMethod("show", String.class);
        System.out.println(method);
        //运行方法
        method.setAccessible(true);
        Student stu = (Student) clazz.newInstance();
        Object invoke = method.invoke(stu, "张三");
        System.out.println(invoke);

    }

    private static void method3(Class clazz) throws NoSuchMethodException {
        Method method = clazz.getMethod("function2", String.class);
        System.out.println(method);
    }

    private static void method2(Class clazz) {
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }

    private static void method1(Class clazz) {
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }
}
